//
//  SecondRegisterViewController.m
//  Socrates
//
//  Created by shy on 15/3/14.
//  Copyright (c) 2015年 shy. All rights reserved.
//

#import "PresentationViewController.h"
#import "LabelTextFieldTableViewCell.h"
#import "LabelChoiceViewCell.h"
#import "NumberLimitationViewController.h"
#import "CircleImage.h"
#import "ContactsViewController.h"

typedef enum {
    kPresent_picture_item = 0,
    kPresent_Items_Num,
} RegisterAccountItems;

typedef enum {
    kPresent_activity_title_item = 0,
    kPresent_activity_location_item,
    kPresent_activity_fee_item,
    kPresent_Interest_Num,
} RegisterInterestItems;

typedef enum {
    kPresent_Information_sTime = 0,
    kPresent_Information_eTime,
    kPresent_Information_limit,
    kPresent_Information_friends,
    kPresent_Information_description,
    kPresent_Information_Num,
} PresentInformationItems;

typedef enum {
    kPresent_type_first_item = 0,
    kPresent_type_second_item,
    kPresent_type_third_item,
    kPresent_type_Num,
} PresentTypeItems;

typedef enum {
    kPresent_Picture_Section = 0 ,
    kPresent_Title_Section,
    kPresent_Information_Section,
    kPresent_Type_Section,
    kPresent_Section_Num,
} PresentTableSectionItems;

static const int kTableViewPortraitTag = 501;
static const int kTableViewNameTag = 502;
static const int kTableViewSchoolTag = 503;
static const int kTableViewGenderTag = 504;
static const int kTableViewBirthTag = 505;
static const int kTableViewNumTag = 506;
static const int kTableViewInviteTag = 507;

//static const int kGenderMaleTag = 11;
//static const int kGenderFemaleTag = 12;

static const int kTableViewHeaderHeightTop = 25;
static const int kTableViewHeaderHeightMidium = 40;
//static const int kNavigationBarHeight = 64;

@interface PresentationViewController ()<UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource> {
    UITextView *activityDescription_;
    UITextField *activityTitle_;
    UITextField *activityLocation_;
    UIView *selectA_;
    UIView *selectB_;
    UIView *selectC_;
    UIButton *inviteFree_;
    UILabel *interestsHint_;
    UIButton *submit_;
    
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

@implementation PresentationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.title = @"发布";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    self.automaticallyAdjustsScrollViewInsets = YES;
//    self.view.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:244/255. alpha:1.];
    self.view.backgroundColor = [UIColor clearColor];
    CGFloat tableviewHeight = 624;
    [self.scrollView setScrollEnabled:YES];
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];

    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.scrollEnabled = NO;
    [self.scrollView addSubview:self.tableView];
    UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, tableviewHeight, self.view.frame.size.width, 1.)];
    bottomLine.backgroundColor = [UIColor colorWithRed:224/255. green:224/255. blue:224/255. alpha:1.];
    [self.scrollView addSubview:bottomLine];
    
    submit_ = [[UIButton alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 160)/2, tableviewHeight + 25, 160, 40)];
    [submit_ setBackgroundColor:[UIColor colorWithRed:252/255. green:108/255. blue:108/255. alpha:1.]];
    [submit_ setTitle:@"立即发布" forState:UIControlStateNormal];
    [submit_ setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.scrollView addSubview:submit_];
}

- (void)configInterestSection {
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    UIButton *selects1_ = [[UIButton alloc] initWithFrame:CGRectMake(10, 7, 30, 30)];
    [selects1_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    UIButton *selects2_ = [[UIButton alloc] initWithFrame:CGRectMake(110, 7, 30, 30)];
    [selects2_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    UIButton *selects3_ = [[UIButton alloc] initWithFrame:CGRectMake(215, 7, 30, 30)];
    [selects3_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    UILabel *interests1_ = [[UILabel alloc] initWithFrame:CGRectMake(40, 11, 60, 22)];
    interests1_.font = [UIFont systemFontOfSize:14.];
    interests1_.textColor = [UIColor grayColor];
    interests1_.text = @"美食旅游";
    UILabel *interests2_ = [[UILabel alloc] initWithFrame:CGRectMake(140, 11, 60, 22)];
    interests2_.textColor = [UIColor grayColor];
    interests2_.font = [UIFont systemFontOfSize:14.];
    interests2_.text = @"讲座沙龙";
    UILabel *interests3_ = [[UILabel alloc] initWithFrame:CGRectMake(245, 11, 60, 22)];
    interests3_.font = [UIFont systemFontOfSize:14.];
    interests3_.textColor = [UIColor grayColor];
    selectA_ = [[UIView alloc] initWithFrame:CGRectMake((width - 320)/2, 0, 320, 36)];
    [selectA_ addSubview:selects1_];
    [selectA_ addSubview:interests1_];
    [selectA_ addSubview:selects2_];
    [selectA_ addSubview:interests2_];
    [selectA_ addSubview:selects3_];
    [selectA_ addSubview:interests3_];
    
    UIButton *selects4_ = [[UIButton alloc] initWithFrame:CGRectMake(10, 7, 30, 30)];
    [selects4_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    UIButton *selects5_ = [[UIButton alloc] initWithFrame:CGRectMake(110, 7, 30, 30)];
    [selects5_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    UIButton *selects6_ = [[UIButton alloc] initWithFrame:CGRectMake(215, 7, 30, 30)];
    [selects6_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    UILabel *interests4_ = [[UILabel alloc] initWithFrame:CGRectMake(40, 11, 60, 22)];
    interests4_.font = [UIFont systemFontOfSize:14.];
    interests4_.textColor = [UIColor grayColor];
    UILabel *interests5_ = [[UILabel alloc] initWithFrame:CGRectMake(140, 11, 60, 22)];
    interests5_.font = [UIFont systemFontOfSize:14.];
    interests5_.textColor = [UIColor grayColor];
    UILabel *interests6_ = [[UILabel alloc] initWithFrame:CGRectMake(245, 11, 60, 22)];
    interests6_.font = [UIFont systemFontOfSize:14.];
    interests6_.textColor = [UIColor grayColor];
    selectB_ = [[UIView alloc] initWithFrame:CGRectMake((width - 320)/2, 0, 320, 36)];
    [selectB_ addSubview:selects4_];
    [selectB_ addSubview:interests4_];
    [selectB_ addSubview:selects5_];
    [selectB_ addSubview:interests5_];
    [selectB_ addSubview:selects6_];
    [selectB_ addSubview:interests6_];
    
    UIButton *selects7_ = [[UIButton alloc] initWithFrame:CGRectMake(10, 7, 30, 30)];
    [selects7_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    UIButton *selects8_ = [[UIButton alloc] initWithFrame:CGRectMake(110, 7, 30, 30)];
    [selects8_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    UILabel *interests7_ = [[UILabel alloc] initWithFrame:CGRectMake(40, 11, 60, 22)];
    interests7_.font = [UIFont systemFontOfSize:14.];
    interests7_.textColor = [UIColor grayColor];
    UILabel *interests8_ = [[UILabel alloc] initWithFrame:CGRectMake(140, 11, 60, 22)];
    interests8_.font = [UIFont systemFontOfSize:14.];
    interests8_.textColor = [UIColor grayColor];
    selectC_ = [[UIView alloc] initWithFrame:CGRectMake((width - 320)/2, 0, 320, 36)];
    [selectC_ addSubview:selects7_];
    [selectC_ addSubview:interests7_];
    [selectC_ addSubview:selects8_];
    [selectC_ addSubview:interests8_];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return kPresent_Section_Num;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    switch (section) {
        case kPresent_Picture_Section:
            return kTableViewHeaderHeightTop;
        case kPresent_Title_Section:
            return kTableViewHeaderHeightTop;
        case kPresent_Information_Section:
            return kTableViewHeaderHeightTop;
        case kPresent_Type_Section:
            return kTableViewHeaderHeightMidium;
        default:
            break;
    }
    return 0;
}

- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section {
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 25)];
    view1.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:244/255. alpha:1.];
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 40)];
    UILabel *interests = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, width - 40, 40)];
    interests.text = @"选择活动的类型";
    interests.font = [UIFont systemFontOfSize:12.];
    interests.textColor = [UIColor grayColor];
    interests.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:244/255. alpha:1.];
    [view2 addSubview:interests];
    view2.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:244/255. alpha:1.];
    switch (section) {
        case kPresent_Picture_Section:
            return view1;
        case kPresent_Title_Section:
            return view1;
        case kPresent_Information_Section:
            return view1;
        case kPresent_Type_Section:
            return view2;
        default:
            break;
    }
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case kPresent_Picture_Section:
            return kPresent_Items_Num;
        case kPresent_Title_Section:
            return kPresent_Interest_Num;
        case kPresent_Information_Section:
            return kPresent_Information_Num;
        case kPresent_Type_Section:
            return kPresent_type_Num;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch ([indexPath section]) {
        case kPresent_Picture_Section:
            switch ([indexPath row]) {
                case kPresent_picture_item:
                    return 65;
                    break;
                    
                default:
                    return 36;
                    break;
            }
        case kPresent_Title_Section:
            return 36;
        case kPresent_Information_Section:
            switch ([indexPath row]) {
                case kPresent_Information_description:
                    return 85;
                    break;
                    
                default:
                    return 36;
                    break;
            }
        case kPresent_Type_Section:
            return 36;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UIEdgeInsets edgeInset = self.tableView.separatorInset;
    self.tableView.separatorInset = UIEdgeInsetsMake(edgeInset.top, 23, edgeInset.bottom, 23);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    NSString *cellIdentifier = @"RegisterCellIdentifier";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    UIImageView *portrait = [[UIImageView alloc] initWithFrame:CGRectMake(width - 100, 3, 60, 60)];
    UIImage *userImage = [CircleImage circleImageWithName:@"angela_test" borderWidth:0 borderColor:[UIColor clearColor]];
    [portrait setImage:userImage];
    inviteFree_ = [[UIButton alloc] initWithFrame:CGRectMake(width - 150, 6, 24, 24)];
    [inviteFree_ setImage:[UIImage imageNamed:@"solid"] forState:UIControlStateSelected];
    [inviteFree_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    [inviteFree_ addTarget:self action:@selector(clickInviteFree:) forControlEvents:UIControlEventTouchUpInside];
    [self configInterestSection];
    switch ([indexPath section]) {
        case kPresent_Picture_Section: {
            switch ([indexPath row]) {
                case kPresent_picture_item:
                    cell.textLabel.text = @"图像";
                    cell.textLabel.font = [UIFont systemFontOfSize:16.];
                    cell.textLabel.textColor = [UIColor lightGrayColor];
                    [cell addSubview:portrait];
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    cell.tag = kTableViewPortraitTag;
                    break;
                default:
                    break;
            }
            
            break;
        }
            
        case kPresent_Title_Section: {
            switch ([indexPath row]) {
                case kPresent_activity_title_item:
                    activityTitle_ = [[UITextField alloc] initWithFrame:CGRectMake(23, 0, width - 46, cell.frame.size.height)];
                    activityTitle_.placeholder = @"活动标题";
                    activityTitle_.font = [UIFont systemFontOfSize:16.];
                    activityTitle_.textColor = [UIColor grayColor];
                    [activityTitle_ setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
                    [cell addSubview:activityTitle_];
                    cell.tag = kTableViewNameTag;
                    break;
                case kPresent_activity_location_item:
                    activityLocation_ = [[UITextField alloc] initWithFrame:CGRectMake(23, 0, width - 46, cell.frame.size.height)];
                    activityLocation_.placeholder = @"活动位置";
                    activityLocation_.font = [UIFont systemFontOfSize:16.];
                    activityLocation_.textColor = [UIColor grayColor];
                    [activityLocation_ setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
                    [cell addSubview:activityLocation_];
                    break;
                case kPresent_activity_fee_item:
                    cell.textLabel.text = @"人均费用";
                    cell.textLabel.font = [UIFont systemFontOfSize:16.];
                    cell.textLabel.textColor = [UIColor lightGrayColor];
                    cell.tag = kTableViewGenderTag;
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    [cell addSubview:inviteFree_];
                    UILabel *hint = [[UILabel alloc] initWithFrame:CGRectMake(width - 120, 6, 80, 24)];
                    hint.text = @"有人请客";
                    hint.textColor = [UIColor lightGrayColor];
                    hint.font = [UIFont systemFontOfSize:16.];
                    hint.textAlignment = NSTextAlignmentLeft;
                    [cell addSubview:hint];
                    break;
            }
            break;
        }
        case kPresent_Information_Section: {
            switch ([indexPath row]) {
                case kPresent_Information_sTime:
                    cell.textLabel.text = @"开始时间";
                    cell.textLabel.font = [UIFont systemFontOfSize:16.];
                    cell.textLabel.textColor = [UIColor lightGrayColor];
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    break;
                case kPresent_Information_eTime:
                    cell.textLabel.text = @"结束时间";
                    cell.textLabel.font = [UIFont systemFontOfSize:16.];
                    cell.textLabel.textColor = [UIColor lightGrayColor];
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    break;
                case kPresent_Information_limit:
                    cell.textLabel.text = @"人数上限";
                    cell.tag = kTableViewNumTag;
                    cell.textLabel.font = [UIFont systemFontOfSize:16.];
                    cell.textLabel.textColor = [UIColor lightGrayColor];
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    break;
                case kPresent_Information_friends:
                    cell.textLabel.text = @"邀请好友";
                    cell.tag = kTableViewInviteTag;
                    cell.textLabel.font = [UIFont systemFontOfSize:16.];
                    cell.textLabel.textColor = [UIColor lightGrayColor];
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    break;
                case kPresent_Information_description:
                    activityDescription_ = [[UITextView alloc] initWithFrame:CGRectMake(23, 0, self.view.frame.size.width - 46, 85)];
                    activityDescription_.font = [UIFont systemFontOfSize:12.];
                    activityDescription_.text = @"填写活动描述，让趣友喜欢你的活动。";
                    activityDescription_.textColor = [UIColor lightGrayColor];
                    [cell addSubview:activityDescription_];
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    break;
                default:
                    break;
            }
            break;
        }
        case kPresent_Type_Section: {
            switch ([indexPath row]) {
                case kPresent_type_first_item:
                    [cell addSubview:selectA_];
                    break;
                case kPresent_type_second_item:
                    [cell addSubview:selectB_];
                    break;
                case kPresent_type_third_item:
                    [cell addSubview:selectC_];
                    break;
                default:
                    break;
            }
        }
    }
    
    return cell;
}

- (void)clickInviteFree:(id)sender {
    NSLog(@"click inviteFree.");
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    switch ([tableView cellForRowAtIndexPath:indexPath].tag) {
        case kTableViewPortraitTag:
            break;
            
        case kTableViewNameTag: {
            break;
        }
        case kTableViewSchoolTag: {
            break;
        }
        case kTableViewGenderTag: {
            break;
        }
        case kTableViewBirthTag: {
            break;
        }
        case kTableViewNumTag: {
            NumberLimitationViewController *nvc = [[NumberLimitationViewController alloc] initWithNibName:@"NumberLimitationViewController" bundle:nil];
            [self.navigationController pushViewController:nvc animated:YES];
            break;
        }
        case kTableViewInviteTag: {
            ContactsViewController *vc = [[ContactsViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        default:
            break;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.scrollView setContentSize:CGSizeMake(320,700)];
}
@end